package com.lanyou.esb.cook.proxy.redis.proxy.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants4Redis;
import com.lanyou.esb.cook.proxy.redis.JedisPoolUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.base.BaseStrJedisProxy;

/**
 * 接口平均响应时间Redis操作代理
 * 
 * @author Davey.wu
 */
public class IfAccTimesJedisProxy extends BaseStrJedisProxy<Integer> {

	public IfAccTimesJedisProxy() {
		this.setTimeOut(Constants4Redis.REDIS_TIMEOUT_IFACCTIMES);
	}

	@Override
	public long setex(String key, Integer value) throws JedisException,
			JsonProcessingException {
		if (value == null) {
			return 0l;
		}
		long result = 0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0;
		}
		boolean broken = false;
		try {
			// 判断键是否存在，不存在则设置过期时间
			if (!jedis.exists(key)) {
				jedis.expire(key, this.getTimeOut());
			}
			result = jedis.incrBy(key, value);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	@Override
	public Integer get(String key) throws JedisException, JsonParseException,
			JsonMappingException, IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0;
		}
		boolean broken = false;
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		try {
			return Integer.parseInt(result);
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 根据键集合获取值
	 * 
	 * @param keys
	 * @return
	 * @throws JedisException
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public Map<String, Integer> getInt(List<String> keys)
			throws JedisException, JsonParseException, JsonMappingException,
			IOException {
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		Map<String, Response<String>> responses = new HashMap<>(keys.size());
		try {
			Pipeline pipe = jedis.pipelined();
			for (String key : keys) {
				responses.put(key, pipe.get(key));
			}
			pipe.sync();
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (responses.isEmpty()) {
			return null;
		}
		Map<String, Integer> result = new HashMap<>(responses.size());
		for (Entry<String, Response<String>> entry : responses.entrySet()) {
			int value = 0;
			try {
				value = Integer.parseInt(entry.getValue().get());
			} catch (Exception e) {
				value = 0;
			}
			result.put(entry.getKey(), value);
		}
		return result;
	}

	public void decrBy(Map<String, Integer> values) throws JedisException,
			JsonParseException, JsonMappingException, IOException {
		if (values == null || values.isEmpty()) {
			return;
		}
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return;
		}
		boolean broken = false;
		try {
			Pipeline pipe = jedis.pipelined();
			for (Entry<String, Integer> entry : values.entrySet()) {
				pipe.incrBy(entry.getKey(), -entry.getValue());
			}
			pipe.sync();
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
	}

	@Override
	public String getKeyFromObject(Integer value) {
		// TODO Auto-generated method stub
		return value + "";
	}
}
